package ui;

import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Image;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import ui.MainUI.BackgroundPanel;
import config.MainConfig;

import listener.LoginListener;

public class LoginUI extends JFrame {
	public JTextField jTextField,temp;// 定义文本框组件
	public JPasswordField jPasswordField;// 定义密码框组件
	JLabel jLabel1, jLabel2;
	JPanel jp1, jp2, jp3;
	public JButton jb1, jb2; // 创建按钮
	// 创建背景面板。
	BackgroundPanel bgp;
	Container ct;

	public LoginUI() throws Exception {
		setResizable(false);
		// 不采用任何布局方式。
		ct = this.getContentPane();
		this.setLayout(null);
		bgp = new BackgroundPanel(
				(new ImageIcon(MainConfig.loginBackPath)).getImage());
		bgp.setBounds(0, 0, 400, 230);
		ct.add(bgp, -1);
		
		jTextField = new JTextField(13);
		temp = new JTextField(6);
		temp.setOpaque(false);
		jPasswordField = new JPasswordField(13);
		jLabel1 = new JLabel("用户名:   ");
		jLabel2 = new JLabel("密    码:   ");
		Font fontButton = new Font("SansSerif", Font.BOLD, 15);
		jLabel1.setFont(fontButton);
		jLabel2.setFont(fontButton);
		
		jb1 = new JButton("确认");
		jb2 = new JButton("取消");
		jb1.setFont(fontButton);
		jb2.setFont(fontButton);
		jp1 = new JPanel();
		jp2 = new JPanel();
		jp3 = new JPanel();

		jp1.setOpaque(false);
		jp2.setOpaque(false);
		jp3.setOpaque(false);
		// 设置布局
		GridLayout gridLayout = new GridLayout(3, 1);

		JPanel jpanelAll = new JPanel();
		jpanelAll.setLayout(gridLayout);
		jpanelAll.setBounds(80, 50, 280, 150);
		jpanelAll.setOpaque(false);
		
		jp1.add(jLabel1);
		jp1.add(jTextField);// 第一块面板添加用户名和文本框

		jp2.add(jLabel2);
		jp2.add(jPasswordField);// 第二块面板添加密码和密码输入框

		jp3.add(temp);
		jp3.add(jb1);
		jp3.add(jb2); // 第三块面板添加确认和取消
		
		jpanelAll.add(jp1);
		jpanelAll.add(jp2);
		jpanelAll.add(jp3);
//		jpanelAll.add(jp4);

		LoginListener listener = new LoginListener(this);
		jb1.addActionListener(listener);
		jb2.addActionListener(listener);
		this.add(jpanelAll,0);
		temp.setEditable(false);
		temp.setEnabled(false);
		temp.setBorder(null);
		// 设置显示
		this.setSize(400, 230);
		this.setDefaultCloseOperation(3);
		this.setLocationRelativeTo(null);
		this.setTitle("欢迎登陆中天模拟键盘服务器");
	}

	public static void main(String[] args) throws Exception{
		new LoginUI().setVisible(true);
	}
	
	/**
	 * 绘制背景图片
	 * 
	 * @author wj
	 * 
	 */
	class BackgroundPanel extends JPanel {
		Image im;

		public BackgroundPanel(Image im) {
			this.im = im;
		}

		public void paintComponent(Graphics g) {
			super.paintComponent(g);
			g.drawImage(im, 0, 0, this.getWidth(), this.getHeight(), this);
		}

	}
}
